home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / FLNORM.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  5KB  |  159 lines

  1. /*
  2. **  FLNORM.C - Normalize DOS file names
  3. **
  4. **  Original Copyright 1988-1991 by Bob Stout as part of
  5. **  the MicroFirm Function Library (MFL)
  6. **
  7. **  This subset version is functionally identical to the
  8. **  version originally published by the author in Tech Specialist
  9. **  magazine and is hereby donated to the public domain.
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <string.h>
  14. #ifdef __TURBOC__
  15.  #include <dir.h>
  16. #else
  17.  #include <direct.h>
  18. #endif
  19. #include <dos.h>
  20. #include <io.h>
  21.  
  22. #define MAX_FLEN 67
  23. #define LAST_CHAR(string) (((char *)string)[strlen(string)-1])
  24.  
  25. typedef enum {ERROR = -1, FALSE, TRUE} LOGICAL;
  26.  
  27. /*
  28. **  NOTE: Uses the following functions, also in SNIPPETS!
  29. */
  30.  
  31. int chdrv(int);                           /* In DRVALID.C   */
  32. char *unix2dos(char *);                   /* In UNIX2DOS.C  */
  33. char *fln_fix(char *);                    /* In FLN_FIX.C   */
  34.  
  35. int flnorm(char *in_name, char *out_name)
  36. {
  37.       LOGICAL dir_flag = FALSE, new_drv = FALSE, root_flag;
  38.       int status = 0, level = 0;
  39.       char *p, *out;
  40.       static char drive[2][3];
  41.       static char file[14];
  42.       static char I_am_here[MAX_FLEN];
  43.       static char I_am_there[MAX_FLEN];
  44.       static char remember[MAX_FLEN];
  45.  
  46.       getcwd(I_am_here, MAX_FLEN);
  47.       if (!in_name || !in_name[0])
  48.       {
  49.             strcpy(out_name, I_am_here);
  50.             goto ERRexit;
  51.       }
  52.       strncpy(drive[0], I_am_here, 2);
  53.       drive[0][2] = '\0';
  54.       if (':' == in_name[1])
  55.       {     /* If a drive is specified                            */
  56.             if (chdrv(in_name[0]))
  57.             {     /* If the drive is invalid                      */
  58.                   status = ERROR;
  59.                   goto ERRexit;
  60.             }
  61.             new_drv = TRUE;
  62.             getcwd(remember, MAX_FLEN);
  63.             strncpy(drive[1], remember, 2);
  64.             drive[1][2] = '\0';
  65.       }
  66.       else
  67.       {     /* If a drive isn't specified                         */
  68.             if (NULL != (p = strchr(in_name, ':')))
  69.             {     /* If filename is illegal                       */
  70.                   status = ERROR;
  71.                   goto ERRexit;
  72.             }
  73.       }
  74.       unix2dos(in_name);
  75.       if (new_drv)
  76.       {
  77.             if ('\\' == in_name[2])
  78.                   strcpy(out_name, drive[1]);
  79.             else
  80.             {
  81.                   strcpy(out_name, remember);
  82.                   if ('\\' != LAST_CHAR(remember))
  83.                         strcat(out_name, "\\");
  84.             }
  85.       }
  86.       else
  87.       {
  88.             strcpy(out_name, drive[0]);
  89.             if ('\\' != *in_name)
  90.             {
  91.                   strcat(out_name, I_am_here);
  92.                   if ('\\' != LAST_CHAR(I_am_here))
  93.                         strcat(out_name, "\\");
  94.             }
  95.       }
  96.       strcat(out_name, &in_name[(new_drv) ? 2 : 0]);
  97.       fln_fix(out_name);
  98.       out = &out_name[2];
  99.       if (!(*out))
  100.             goto ERRexit;
  101.       while ('\\' == LAST_CHAR(out))
  102.       {     /* Strip trailing `\'s                                */
  103.             LAST_CHAR(out_name) = '\0';
  104.             dir_flag = TRUE;
  105.       }
  106.       if (!(*out))
  107.       {
  108.             if (dir_flag)
  109.             {
  110.                   strcat(out, "\\");
  111.                   goto ERRexit;
  112.             }
  113.             else  goto BADPATH;
  114.       }
  115.       if (NULL != (p = strrchr(out_name, '\\')))
  116.             strcpy(file, p);        /* Save filename              */
  117.       if (chdir(out))
  118.       {     /* If can't move to path                              */
  119.             if ((!dir_flag) && p)
  120.             {     /* If there was a separate path                 */
  121.                   *p = '\0';
  122.                   if (!(*out))
  123.                   {     /* Back at the root, handle it            */
  124.                         strcpy(p, "\\");
  125.                         strcpy(file, &file[1]);
  126.                   }
  127.                   if (chdir(out))
  128.                   {     /* If we can't move to path               */
  129.                         *p = '\\';
  130.                         goto BADPATH;
  131.                   }
  132.                   ++level;          /* Flag we stripped name      */
  133.             }
  134.             else
  135.             {     /* No path as specified                         */
  136.                   if (p)
  137.                   {
  138. BADPATH:                status = ERROR;
  139.                         goto ERRexit;
  140.                   }
  141.             }
  142.       }
  143.       getcwd(I_am_there, MAX_FLEN); /* Get normalized path        */
  144.       strupr(I_am_there);
  145.       strcpy(out_name, I_am_there);
  146.       if (level)
  147.             strcat(out_name, file);
  148. ERRexit:
  149.       if (new_drv)
  150.       {
  151.             chdir(remember);
  152.             chdrv(I_am_here[0]);
  153.       }
  154.       chdir(I_am_here);
  155.       if (status)
  156.             out_name[0] = '\0';
  157.       return status;
  158. }
  159.